<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

$THEME->addBreadcrumb($this->adminUrl.'/domains/root/', $this->lang('ap_title_domains_root'));


if(Antz_IntelliForm::submitted('root-page') || (get('root-page')==1 && get('domain')!='')){
    // wanting to set the new root page
    if(get('page')!=''){
        // have selected the new page
        $domainid = (int) get('domain');
        $newdomainid = (int) get('page');
        if($DB->countRows($CONFIG->dbprefix.'pages', "id = {$domainid}")==0){
            JFX::addError($this->lang('invalid_domain'));
            JFX::redirect($CONFIG->adminUrl.'/domains');
        }
        if($DB->countRows($CONFIG->dbprefix.'pages', "id = {$newdomainid}")==0){
            JFX::addError($this->lang('invalid_domain'));
            JFX::redirect($CONFIG->adminUrl.'/domains');
        }
        $existingDomain = $DB->oneValue($CONFIG->dbprefix.'pages', 'id', "parent_id = 0 AND domain_id = '{$domainid}'");
        $DB->update($CONFIG->dbprefix.'pages', array('parent_id' => $newdomainid), "id = '{$existingDomain}'");
        $DB->update($CONFIG->dbprefix.'pages', array('parent_id' => 0, 'domain_id' => $domainid), "id = '{$newdomainid}'");
        JFX::addSuccess($this->lang('reassign_domain_success'));
        JFX::redirect($CONFIG->adminUrl.'/domains');
    }else{
        // have to select the new page
        $pagesRecursive = JFX::getPageTree(0, $USER->workspace);
        $tree = '<ul id="jfxcore-domains-page-tree">'.JFX_Domains_MakeTree($pagesRecursive, post('domain')).'</ul>';

        $js = '
        $(document).ready(function(){
            $("#jfxcore-domains-page-tree").treeview({
                persist: "location",
                collapsed: true,
                unique: true
            });
        });
        ';

        $VIEW->addJs($js);

        echo '<h1>'.$this->lang('new_root_domain_heading').'</h1>';
        echo '<p>'.$this->lang('new_root_domain_description').'</p>';

        echo $tree;
        return;
    }
}






$rootFormFields = array(
    array(
        'name' => 'domain',
        'value' => post('domain'),
        'options' => $domainOpts,
        'label' => $this->lang('domain'),
        'type' => 'select'
    ),
    array(
        'name' => 'submitBtn',
        'type' => 'submit',
        'value' => $this->lang('assign_root_domain_submit'),
        'label' => '&nbsp;'
    )
);

$rootForm = JFX::makeRapidForm($rootFormFields, $this->lang('assign_root_domain_legend'), 'root-page');

echo '<div id="jfx-root-page">'.$rootForm->render().'</div>';
echo '<div class="jfx-clear"></div>';





